<?php
include_once("../../untils/conn.php");

// 确认数据库连接成功
if (!$con) {
    die("数据库连接失败：" . mysqli_connect_error());
}

mysqli_query($con, "set names utf8");

$sql = "SELECT * FROM kefu";
$res = mysqli_query($con, $sql);
if(mysqli_num_rows($res) > 0){
    $row = mysqli_fetch_assoc($res);
    $user_id = $row['appid'];
    $secret = $row['key'];
}

// 查询符合条件的订单号
$sql = "SELECT oderid FROM oder WHERE card_sta = 0 AND status NOT IN (0, 4) AND api_type = 'yqrapi'";
$res = mysqli_query($con, $sql);
$order_ids = array();
while ($row = mysqli_fetch_assoc($res)) {
$order_ids[] = $row['oderid'];
}

// 遍历订单号数组，查询每个订单的信息并打印
foreach ($order_ids as $order_id) {
    $timestamp = time();
    $down_order_id = $order_id;
    $sign = md5("DownOrderID=" . $down_order_id . "&Timestamp=" . $timestamp . "&user_id=" . $user_id . $secret);

    // 发送 POST 请求
    $url = "https://haokaopenapi.lot-ml.com/api/order/GetOrderInfo";
    $data = array(
        'user_id' => $user_id,
        'Timestamp' => $timestamp,
        'DownOrderID' => $down_order_id,
        'user_sign' => $sign
    );
    $options = array(
        'http' => array(
            'header' => "Content-type: application/x-www-form-urlencoded",
            'method' => 'POST',
            'content' => http_build_query($data),
        ),
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

     // 处理返回结果
    $result_arr = json_decode($result, true);
    if ($result_arr['code'] == 0) {
        $order_info = $result_arr['data'];
        $downOrderNo = $order_info['downOrderNo'];
        $expressName = $order_info['expressName'];
        $expressCode = $order_info['expressCode'];
        $orderStatus = $order_info['orderStatus'];
        $thirdPhone = $order_info['thirdPhone'];
        $remark = $order_info['remark'];
        $cardStatus = $order_info['cardStatus'];
    
        // 根据订单状态更新数据库中的订单信息
        if ($orderStatus == '审核不通过') {
            $update_sql = "UPDATE oder SET status=4, beizhu='$remark' WHERE oderid='$downOrderNo'";
            $update_sql_revenue = "UPDATE revenue SET oder_sta=4 WHERE oderid='$downOrderNo'";
        } else if ($orderStatus == '已发货') {
            $update_sql = "UPDATE oder SET status=2, beizhu='$expressName $expressCode', thirdphone='$thirdPhone' WHERE oderid='$downOrderNo'";
            $update_sql_revenue = "UPDATE revenue SET oder_sta=2 WHERE oderid='$downOrderNo'";
            // 在已发货状态下判断cardStatus是否为已激活，如果是则更新card_sta为1
            if ($cardStatus == '已激活') {
                $update_sql .= ", card_sta=1";
            }
        } else if ($orderStatus == '已完成') {
            $update_sql = "UPDATE oder SET status=3, card_sta=1 WHERE oderid='$downOrderNo'";
            $update_sql_revenue = "UPDATE revenue SET oder_sta=3 WHERE oderid='$downOrderNo'";
        } else {
            echo "订单 " . $downOrderNo . " 状态异常：" . $orderStatus . "\n";
            continue;
        }
    
        $update_res = mysqli_query($con, $update_sql);
        $update_res_revenue = mysqli_query($con, $update_sql_revenue);
        if ($update_res && $update_res_revenue) {
            echo "订单 " . $downOrderNo . " 更新成功\n";
        } else {
            echo "订单 " . $downOrderNo . " 更新失败：" . mysqli_error($con) . "\n";
        }


        } else {
            echo "查询订单 " . $order_id . " 失败：" . $result_arr['message'] . "\n";
        }


}

